<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * @author Pisyek Kumar
 * @email pisyek@gmail.com
 * @link http://www.pisyek.com
 */
class Blog extends CI_Controller {

    public $ruta_template;

    function __construct() {
        parent::__construct();
        $this->lang->load('lang/blg_'.$this->config->item('language'), $this->config->item('language'));
        $this->load->model('blog_model');
        $this->ruta_template = array('ruta' => site_url() . 'public/' . $this->template->getActiveTemplate(),
            'ruta_archivos' => 'templates/traveltemplate/');
        $this->template->write_view('footer', 'footer');
        $this->template->write_view('header', 'header');
        $this->template->write_view('_scripts', 'js', $this->ruta_template);
        $this->template->write_view('_styles', 'css', $this->ruta_template);  
    }

    public function index() {
        $pages = 5;

        $data['posts'] = $this->blog_model->get_posts($pages, $this->uri->segment(2));
        $data['current'] = 'HOME';
        $data['categories'] = $this->blog_model->get_categories();

        $this->load->library('pagination'); //Cargamos la librería de paginación
        $config['uri_segment'] = 2;
        $config['base_url'] = site_url('blog'); // parametro base de la aplicación, si tenemos un .htaccess nos evitamos el index.php
        $config['per_page'] = $pages; 
        
        $config['num_links'] = 3; //Numero de links mostrados en la paginación
        $config['total_rows'] = $this->blog_model->get_num_posts();
        $config['full_tag_open'] = "<ul class='pagination'>";
        $config['full_tag_close'] = "</ul>";
        $config['next_tag_open'] = "<li class='next'>";
        $config['next_tag_close'] = "</li>";
        $config['prev_tag_open'] = "<li class='prev'>";
        $config['prev_tag_close'] = "</li>";
        $config['cur_tag_open'] = '<li class="active"><a href="#">';
        $config['cur_tag_close'] = '</a></li>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        $this->pagination->initialize($config); 
        // render view
        $this->template->write_view('content', 'blog/index', $data);
        $this->template->render();
    }

    public function about() {
        $data['current'] = 'ABOUT';
        $data['categories'] = $this->blog_model->get_categories();
        $this->template->write_view('content', 'blog/about', $data);
        $this->template->render();
    }

    public function post($id) { // get a post based on id
        $pages = 5; //Numero de registros mostrados por páginas
        $result = $this->blog_model->get_post($id);
        $data['query'] = $result;
        $data['comments'] = $this->blog_model->get_post_comment($id, $pages, $this->uri->segment(4)); // get comments related to the post
        $data['post_id'] = $id;
        $data['categories'] = $this->blog_model->get_categories();
        $data['ruta_template'] = $this->ruta_template;
        
        $this->load->library('pagination'); //Cargamos la librería de paginación
        $config['uri_segment'] = 4;
        $config['base_url'] = site_url('blog/post/'.$id); // parametro base de la aplicación, si tenemos un .htaccess nos evitamos el index.php
        $config['per_page'] = $pages; 
        $config['num_links'] = 3; //Numero de links mostrados en la paginación
        $config['total_rows'] = $result[0]->comment_count;
        $config['full_tag_open'] = "<ul class='pagination'>";
        $config['full_tag_close'] = "</ul>";
        $config['next_tag_open'] = "<li class='next'>";
        $config['next_tag_close'] = "</li>";
        $config['prev_tag_open'] = "<li class='prev'>";
        $config['prev_tag_close'] = "</li>";
        $config['cur_tag_open'] = '<li class="active"><a href="#">';
        $config['cur_tag_close'] = '</a></li>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        
        $this->pagination->initialize($config); 
        
        $this->load->helper('form');

        if ($this->blog_model->get_post($id)) {
            $this->template->write_view('content', 'blog/post', $data);
            $this->template->render();
        }
        else
            show_404();
    }

    public function add_new_entry() {
        if($this->session->userdata('idusuario') == '' || $this->session->userdata('rol') > 2) { // block un-authorized access
            show_404();
        } else {
            $this->load->helper('form');
            $data['categories'] = form_dropdown('p_categories', $this->blog_model->get_categories(true), '', 'id="p_lugares" style="display:none;" multiple="multiple"');

            $this->load->helper('form');
            $this->load->library('form_validation');

            //set validation rules
            $this->form_validation->set_rules('entry_name', 'Title', 'required|max_length[200]|xss_clean');
            $this->form_validation->set_rules('entry_body', 'Body', 'required|xss_clean');
            $this->form_validation->set_rules('p_valores_lugares', 'Category', 'required');

            if ($this->form_validation->run() == FALSE) {
                $this->template->write_view('content', 'blog/add_new', $data);
                $this->template->render();
            } else {
                //if valid
                $title = $this->input->post('entry_name');
                $body = $this->input->post('entry_body');
                $categories = $this->input->post('p_valores_lugares');

                $this->blog_model->add_new_entry($this->session->userdata('idusuario'), $title, $body, explode(",", $categories));
                $this->session->set_flashdata('message', '1 new post added!');
                redirect('blog/add_new');
            }
        }
    }

    public function add_new_category() {
        if($this->session->userdata('idusuario') == '' || $this->session->userdata('rol') > 2) { // block un-authorized access
            show_404();
        } else {
            $this->load->helper('form');
            $this->load->library('form_validation');

            // set validation rules
            $this->form_validation->set_rules('category_name', 'Nombre', 'required|max_length[200]|xss_clean');
            $this->form_validation->set_rules('category_slug', 'Etiqueta', 'max_length[200]|xss_clean');

            if ($this->form_validation->run() == FALSE) {
                //if not valid
                $data['categories'] = $this->blog_model->get_categories();
                $this->template->write_view('content', 'blog/add_new_category', $data);
                $this->template->render();
            } else {
                //if valid
                $name = $this->input->post('category_name');

                if ($this->input->post('category_slug') != '')
                    $slug = $this->input->post('category_slug');
                else
                    $slug = strtolower(preg_replace('/[^A-Za-z0-9_-]+/', '-', $name));

                $this->blog_model->add_new_category($name, $slug);
                $this->session->set_flashdata('message', '1 new category added!');
                redirect('blog/add_new_category');
            }
        }
    }

    public function category($slug = FALSE) {
        $data['title'] = 'Category - ' . $this->config->item('site_title', 'ion_auth');
        $data['categories'] = $this->blog_model->get_categories();

        if ($slug == FALSE)
            show_404();
        else {
            $data['category'] = $this->blog_model->get_category(NULL, $slug); // get category details
            $data['query'] = $this->blog_model->get_category_post($slug); // get post in the category
            $data['years'] = $this->blog_model->get_years_post(); // get post in the category
        }
        $this->template->write_view('content', 'blog/category', $data);
        $this->template->render();
    }
    
    public function year($year = false, $month = false) {
        $data['title'] = 'Category - ' . $this->config->item('site_title', 'ion_auth');
        $data['categories'] = $this->blog_model->get_categories();

        if ($year == false && $month == false)
            show_404();
        else {
            $data['query'] = $this->blog_model->get_category_post(null, $year, $month); // get post in the category
            $data['years'] = $this->blog_model->get_years_post(); // get post in the category
        }
        $this->template->write_view('content', 'blog/category', $data);
        $this->template->render();
    }
    
    function add_comment(){
        if($this->session->userdata('idusuario') == '') { // block un-authorized access
            show_404();
        } else {
            $this->load->library('form_validation');
            $this->form_validation->set_rules('p_name', lang('nombre'), 'required|max_length[200]|xss_clean');
            $this->form_validation->set_rules('p_email', lang('email'), 'required|max_length[200]|xss_clean');
            $this->form_validation->set_rules('p_website', lang('pagina_web'), 'required|max_length[200]|xss_clean');
            $this->form_validation->set_rules('p_comment', ucfirst(lang('comentario')), 'required|xss_clean');
            if ($this->form_validation->run() == FALSE) {
                $this->session->set_flashdata('message', '1 new comment added!');
                redirect('post/'.$this->input->post('p_post'));
            } else {
                $this->blog_model->add_new_comment($this->input->post('p_post'),
                                                   $this->input->post('p_name'),
                                                   $this->input->post('p_email'),
                                                   $this->input->post('p_comment'),
                                                   $this->input->post('p_website'));
                $this->session->set_flashdata('message', '1 new comment added!');
                redirect('post/'.$this->input->post('p_post'));
            }
        }
    }
    
    public function delete_post($id){
        $this->blog_model->delete_post($id);
        redirect('blog');
    }
}

/* End of file blog.php */
/* Location: ./application/controllers/blog.php */